"ui";
if (device.device == 'le_x620') {
    ui.layout(
        <frame bg="#faad14" gravity="center">
            <text textStyle="bold" marginTop="-100" w="auto" h="100" text="直播助手" color="#f5f5f5" textSize="30" />
            <button h="70" w="110" marginLeft="-130" id="device" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="显示设备码" />
            <button h="70" w="110" marginLeft="1" id="bind" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="绑定设备" />
            <button h="70" w="110" marginLeft="130" id="rebind" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="换绑设备" />

            <button h="70" w="110" marginTop="120" marginLeft="-130" id="start" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="启动" />
            <button h="70" w="110" marginTop="120" marginLeft="130" id="close" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="停止" />

            <text id="version" marginTop="320" marginLeft="150" textStyle="bold" w="150" h="50" textColor="#ffffff" />
        </frame>
    );
} else if (device.device == 'mido') {
    ui.layout(
        <frame bg="#faad14" gravity="center">
            <text textStyle="bold" marginTop="-100" w="auto" h="100" text="直播助手" color="#f5f5f5" textSize="30" />

            <button h="50" w="90" marginLeft="-110" id="device" bg="#aaa9a9" textSize="14sp" textStyle="bold" textColor="#ffffff" text="显示设备码" />
            <button h="50" w="90" marginLeft="1" id="bind" bg="#aaa9a9" textSize="14sp" textStyle="bold" textColor="#ffffff" text="绑定设备" />
            <button h="50" w="90" marginLeft="110" id="rebind" bg="#aaa9a9" textSize="14sp" textStyle="bold" textColor="#ffffff" text="换绑设备" />

            <button h="50" w="90" marginTop="100" marginLeft="-110" id="start" bg="#aaa9a9" textSize="14sp" textStyle="bold" textColor="#ffffff" text="启动" />
            <button h="50" w="90" marginTop="100" marginLeft="110" id="close" bg="#aaa9a9" textSize="14sp" textStyle="bold" textColor="#ffffff" text="停止" />

            <text id="version" marginTop="300" marginLeft="140" textStyle="bold" w="150" h="50" textColor="#ffffff" />
        </frame>
    );
} else if (device.device == 'HWSLA-Q') {
    ui.layout(
        <frame bg="#faad14" gravity="center">
            <text textStyle="bold" marginTop="-100" w="auto" h="100" text="直播助手" color="#f5f5f5" textSize="30" />

            <button h="40" w="80" marginLeft="-100" id="device" bg="#aaa9a9" textSize="12sp" textStyle="bold" textColor="#ffffff" text="显示设备码" />
            <button h="40" w="80" marginLeft="1" id="bind" bg="#aaa9a9" textSize="12sp" textStyle="bold" textColor="#ffffff" text="绑定设备" />
            <button h="40" w="80" marginLeft="100" id="rebind" bg="#aaa9a9" textSize="12sp" textStyle="bold" textColor="#ffffff" text="换绑设备" />

            <button h="40" w="80" marginTop="90" marginLeft="-100" id="start" bg="#aaa9a9" textSize="12sp" textStyle="bold" textColor="#ffffff" text="启动" />
            <button h="40" w="80" marginTop="90" marginLeft="100" id="close" bg="#aaa9a9" textSize="12sp" textStyle="bold" textColor="#ffffff" text="停止" />

            <text id="version" marginTop="280" marginLeft="150" textStyle="bold" w="150" h="50" textColor="#ffffff" />
        </frame>
    );
} else {
    ui.layout(
        <frame bg="#faad14" gravity="center">
            <text textStyle="bold" marginTop="-100" w="auto" h="100" text="直播助手" color="#f5f5f5" textSize="30" />

            <button h="70" w="110" marginLeft="-130" id="device" bg="#aaa9a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="显示设备码" />
            <button h="70" w="110" marginLeft="1" id="bind" bg="#aaa9a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="绑定设备" />
            <button h="70" w="110" marginLeft="130" id="rebind" bg="#aaa9a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="换绑设备" />

            <button h="70" w="110" marginTop="120" marginLeft="-130" id="start" bg="#aaa9a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="启动" />
            <button h="70" w="110" marginTop="1220" marginLeft="130" id="close" bg="#aaa9a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="停止" />

            <text id="version" marginTop="320" marginLeft="150" textStyle="bold" w="150" h="50" textColor="#ffffff" />
        </frame>
    );
}

const DEVICE_ID = 'deviceId';
const APP_VERSION = '1.0.2';
const DOWNLOAD = 'http://fir.baxi.huidaps.cn/wtk5';
let radioText = "抖音";
let running = false;

threads.start(function () {
    let ret = getAppVersion();
    ui.run(function () {
        ui.version.setText('当前版本: ' + ret.version);
    });
    if (ret.version != APP_VERSION) {
        rawInput("有新的版本 " + APP_VERSION + "，请前往下载: ", ret.url);
    }
    let bindRet = getBindInfo();
    if (bindRet.ret) {
        if (!isEmpty(bindRet.data.mobile)) {
            ui.run(function () {
                if (device.device == 'mido') {
                    ui.bind.textSize = "10";
                } else if (device.device == 'HWSLA-Q') {
                    ui.bind.textSize = "8";
                } else {
                    ui.bind.textSize = "12";
                }
                ui.bind.setText('设备号 \n' + bindRet.data.mobile);
            });
        }
        if (!isEmpty(bindRet.data.wx)) {
            ui.run(function () {
                if (device.device == 'mido') {
                    ui.bindWechat.textSize = "10";
                } else if (device.device == 'HWSLA-Q') {
                    ui.bindWechat.textSize = "8";
                } else {
                    ui.bindWechat.textSize = "12";
                }
                ui.bindWechat.setText('引流微信 \n' + bindRet.data.wx);
            });
        }
        if (!isEmpty(bindRet.data.douyin)) {
            ui.run(function () {
                if (device.device == 'mido') {
                    ui.bindTickTok.textSize = "10";
                } else if (device.device == 'HWSLA-Q') {
                    ui.bindTickTok.textSize = "8";
                } else {
                    ui.bindTickTok.textSize = "12";
                }
                ui.bindTickTok.setText('绑定主号 \n' + bindRet.data.douyin);
            });
        }
    }
});

/**
 * 运行脚本
 */
ui.start.click(() => {
    if (running) {
        toast('脚本已开始运行，请勿重复点击!');
        return;
    }
    let dos = function () {
        let script = http.get("http://phone.leyouhome.cn/live.js");
        let scr = script.body.string();
        engines.execScript("main", scr);
    }
    threads.start(dos);
    running = true;
});

/**
 * 停止脚本
 */
ui.close.click(() => {
    threads.start(stop);
});

/**
 * 展示设备码
 */
ui.device.click(() => {
    showImei();
});

/**
 * 绑定设备
 */
ui.bind.click(() => {
    //获取绑定状态
    threads.start(function () {
        let ret = checkBind();
        if (ret.ret) { //可以绑定
            let phone = rawInput("请输入电话号", "");
            //校验手机号
            if (!(/^1[2|3|4|5|6|7|8|9]\d{11}$/.test(phone))) {
                toast("手机号码有误，请重填");
                return;
            }
            let bindRet = bindPhone(phone);
            if (bindRet.ret) {
                toast('绑定成功');
                ui.run(function () {
                    if (device.device == 'mido') {
                        ui.bind.textSize = "10";
                    } else if (device.device == 'HWSLA-Q') {
                        ui.bind.textSize = "8";
                    } else {
                        ui.bind.textSize = "12";
                    }
                    ui.bind.setText('设备号 \n' + phone);
                });
            } else {
                toast(bindRet.msg);
            }
        } else { //已经绑定了
            toast(ret.msg);
        }
    });
});

/**
 * 换绑设备
 */
ui.rebind.click(() => {
    rawInput("请输入新手机号", "").then(phone => {
        //校验手机号
        if (!(/^1[2|3|4|5|6|7|8|9]\d{11}$/.test(phone))) {
            toast("手机号码有误，请重填");
            return;
        }
        threads.start(function () {
            let bindRet = reBindPhone(phone);
            if (bindRet.ret) {
                toast('换绑成功');
                let bindRet = getBindInfo();
                if (bindRet.ret) {
                    ui.run(function () {
                        if (!isEmpty(bindRet.data.mobile)) {
                            ui.run(function () {
                                if (device.device == 'mido') {
                                    ui.bind.textSize = "10";
                                } else if (device.device == 'HWSLA-Q') {
                                    ui.bind.textSize = "8";
                                } else {
                                    ui.bind.textSize = "12";
                                }
                                ui.bind.setText('设备号 \n' + bindRet.data.mobile);
                            });
                        }
                        if (!isEmpty(bindRet.data.wx)) {
                            ui.run(function () {
                                if (device.device == 'mido') {
                                    ui.bindWechat.textSize = "10";
                                } else if (device.device == 'HWSLA-Q') {
                                    ui.bindWechat.textSize = "8";
                                } else {
                                    ui.bindWechat.textSize = "12";
                                }
                                ui.bindWechat.setText('引流微信 \n' + bindRet.data.wx);
                            });
                        }
                        if (!isEmpty(bindRet.data.douyin)) {
                            ui.run(function () {
                                if (device.device == 'mido') {
                                    ui.bindTickTok.textSize = "10";
                                } else if (device.device == 'HWSLA-Q') {
                                    ui.bindTickTok.textSize = "8";
                                } else {
                                    ui.bindTickTok.textSize = "12";
                                }
                                ui.bindTickTok.setText('绑定主号 \n' + bindRet.data.douyin);
                            });
                        }
                    });
                }
            } else {
                toast(bindRet.msg);
            }
        });
    });
});

/**
 * 显示设备码
 */
function showImei() {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    alert('您的设备码是: ' + deviceId);
}


/**
 * 上报设备码
 * @param {string} code 
 */
function reportImei() {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    http.get("http://db.leyouapp.cn:8880/member-phone/add?phoneCode=" + deviceId);
}

/**
 * 停止运行清空信息
 */
function stop() {
    console.clear();
    console.hide();
    engines.stopAll();
}

/**
 * 检查绑定状态
 * @param {string} cate_id 
 */
function checkBind() {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    let url = "http://dy.leyouhome.cn/api/device/device?number=" + deviceId;
    let res = http.get(url);
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                ret: false,
                msg: '已经绑定了: ' + resp.data.number
            }
        } else {
            return {
                ret: true,
                msg: resp.message
            }
        }
    } else {
        return {
            ret: false,
            msg: '当前网络忙'
        }
    }
}

/**
 * 绑定手机号
 * @param {*} phone
 */
function bindPhone(phone) {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    let url = "http://dy.leyouhome.cn/api/device/bind?number=" + deviceId + "&cate_id=" + phone;
    let res = http.get(url);
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                ret: true,
                msg: ''
            }
        } else {
            return {
                ret: false,
                msg: resp.message
            }
        }
    } else {
        return {
            ret: false,
            msg: '当前网络忙'
        }
    }
}

/**
 * 换绑设备
 * @param {*} phone
 */
function reBindPhone(phone) {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    let url = "http://dy.leyouhome.cn/api/device/change?number=" + deviceId + "&cate_id=" + phone;
    log(url)
    let res = http.get(url);
    log(res)
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                ret: true,
                msg: ''
            }
        } else {
            return {
                ret: false,
                msg: resp.message
            }
        }
    } else {
        return {
            ret: false,
            msg: '当前网络忙'
        }
    }
}

/**
 * 获取APP版本信息
 */
function getAppVersion() {
    let url = "http://dy.leyouhome.cn/api/device/version";
    let res = http.get(url);
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                version: resp.data.version,
                url: resp.data.url
            }
        } else {
            return {
                version: APP_VERSION,
                url: DOWNLOAD
            }
        }
    } else {
        return {
            version: APP_VERSION,
            url: DOWNLOAD
        }
    }
}

/**
 * 获取已经绑定的信息
 */
function getBindInfo() {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    let url = "http://dy.leyouhome.cn/api/device/info?device=" + deviceId;
    let res = http.get(url);
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                ret: true,
                data: {
                    mobile: resp.data.mobile,
                    wx: resp.data.wx,
                    douyin: resp.data.douyin
                }
            }
        } else {
            return {
                ret: false
            }
        }
    } else {
        return {
            ret: false
        }
    }
}

/**
 * 工具函数 判断是否为空
 * @param {*} obj 
 */
function isEmpty(obj) {
    if (typeof obj == "undefined" || obj == null || obj == "") {
        return true;
    } else {
        return false;
    }
}